import 'package:beautyflutter/api/request.dart';
import 'package:beautyflutter/api/route.dart';
import 'package:beautyflutter/bean/news_list.dart';
import 'package:beautyflutter/common/loading.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NewsList extends StatefulWidget {
  final newsCultureTypeId;
  const NewsList({Key key, this.newsCultureTypeId}) : super(key: key);
  @override
  _NewsListState createState() => _NewsListState();
}

class _NewsListState extends State<NewsList> with AutomaticKeepAliveClientMixin {
  ScrollController _scrollController = new ScrollController();
  bool isLoading = false;
  bool offState = false;//是否显示进入页面时的圆形进度条
  int _pageNo;
  List<NewsCultureList> newsList = new List<NewsCultureList>();
  @override
  void initState() {
    _initData();
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _pageNo++;
        _getMoreData();
      }
    });
  }
  void _initData() {
    _pageNo=1;
    newsList=[];
    _getMoreData();
  }
  void _getMoreData() {
    Map<String, String> map = new Map();
    map["userId"] = "16990";
    map["pageNo"] = _pageNo.toString();
    map["newsCultureTypeId"] = widget.newsCultureTypeId.toString();
    HttpController.post(RouteUtil.getNewsCultureList, (data) {
      if (data != null) {
        NewsListBean newsListBean = NewsListBean.fromJson(data);
        setState(() {
          isLoading = false;
          offState = true;
          newsList.addAll(newsListBean.newsCultureList);
        });
      }
    }, params: map);
  }
  void doPraise(int objId) {
    Map<String, String> map = new Map();
    map['userId'] = '16990';
    map['objId'] = objId.toString();
    map['praiseType'] = '9';
    HttpController.post(RouteUtil.addPraise, (data) {
      if (data != null) {
        newsList.forEach((item) {
          if(item.newsCultureId == objId) {
            if(data['isPraise'] == 1) {
              item.isPraise = 1;
              item.praiseNum++;
            } else {
              item.isPraise = 0;
              item.praiseNum--;
            }
          }
        });
        setState(() {
          newsList = newsList;
        });
      }
    }, params: map);
  }
  @override
  Widget build(BuildContext context) {
    super.build(context);
    Size size = MediaQuery.of(context).size;
    return Stack(
      children: <Widget>[
        RefreshIndicator(
          onRefresh: () async {
            _initData();
          },
          child: ListView.builder(
            controller: _scrollController,
            itemCount: newsList.length,
            itemBuilder: (BuildContext context, int index) {
              return Padding(
                padding: EdgeInsets.only(bottom: 10),
                child: Column(
                  children: <Widget>[
                    Container(
                      width: size.width,
                      child: Image.network(newsList[index].imgUrl, fit: BoxFit.cover, height: 186),
                    ),
                    Container(
                        width: size.width,
                        decoration: BoxDecoration(color: Colors.white),
                        child: Padding(
                          padding: EdgeInsets.all(10),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.stretch,
                            children: <Widget>[
                              Text(newsList[index].remark, style: TextStyle(fontSize: 16, color: Colors.black54), overflow: TextOverflow.ellipsis),
                              SizedBox(height: 15),
                              Container(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    Text(newsList[index].updateTime),
                                    Row(
                                      children: <Widget>[
                                        GestureDetector(
                                          onTap: () {
                                            print('评论');
                                          },
                                          child: Row(
                                            children: <Widget>[
                                              Icon(Icons.comment, size: 20, color: Colors.grey),
                                              SizedBox(width: 5),
                                              Text(newsList[index].evaluationNum.toString()),
                                            ],
                                          ),
                                        ),
                                        SizedBox(width: 20),
                                        GestureDetector(
                                          onTap: () {
                                            doPraise(newsList[index].newsCultureId);
                                          },
                                          child: Row(
                                            children: <Widget>[
                                              Icon(Icons.thumb_up, size: 20, color: newsList[index].isPraise == 1 ? Colors.deepOrange : Colors.grey),
                                              SizedBox(width: 5),
                                              Text(newsList[index].praiseNum.toString())
                                            ],
                                          )
                                        )
                                      ],
                                    )
                                  ],
                                ),
                              )
                            ],
                          ),
                        )
                    )
                  ],
                ),
              );
            }
          ),
        ),
        Offstage(
          offstage: offState,
          child: LoadingDialog(text: '正在加载...'),
        )
//        Offstage(
//          offstage: offState,
//          child: Center(
//            child: Container(
//              width: 100,
//              height: 100,
//              decoration: BoxDecoration(color: Colors.black38),
//              child: new Column(children: <Widget>[
//                CircularProgressIndicator(),
//                new Text('正在加载...',style: TextStyle(fontSize: 12,color: Colors.white),softWrap: false,)
//              ],mainAxisAlignment: MainAxisAlignment.spaceEvenly),
//            )
//          )
//        ),
      ],
    );
  }

  @override
  void dispose() {
    super.dispose();
    _scrollController.dispose();
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
